package com.jkl.webserver.Web.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jkl.webserver.Tool.CopyFile;
import com.jkl.webserver.Tool.FileCreator;
import com.jkl.webserver.Tool.xFile;
import com.jkl.webserver.Web.DAO.mapper.AccountMapper;
import com.jkl.webserver.Web.DAO.pojo.Account;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class WebRegisterService {
    private  final  xFile xfile;
    private final  CopyFile copyFile;
    private final FileCreator fileCreator;
    private final AccountMapper accountMapper;

    public WebRegisterService(xFile xfile, CopyFile copyFile, FileCreator fileCreator, AccountMapper accountMapper) {
        this.xfile = xfile;
        this.copyFile = copyFile;
        this.fileCreator = fileCreator;
        this.accountMapper = accountMapper;
    }

    public boolean WebRegister(String username,String password,String email) throws Exception {
        QueryWrapper<Account> wrapper=new QueryWrapper<>();
        wrapper.eq("email",email);
        accountMapper.selectCount(wrapper);
        if(accountMapper.selectCount(wrapper)==0)
        {
            accountMapper.insert(new Account(email, password, username));
            createUser(email);
            return true;
        }
        else
        {
            return false;
        }
    }
    private void createUser(String key) throws Exception//创建用户基本空间存储(改)
    {
        File basePath=new File(xfile.getBasePath()+"/userRes");
        fileCreator.createDir(basePath,key);
        File userPath=new File(basePath,key);
        fileCreator.createDir(userPath,"head_img");
        fileCreator.createDir(userPath,"album");
        fileCreator.createDir(userPath,"background");
        fileCreator.createDir(userPath,"userDynamic");
        copyFile.CopyALLFile(basePath+"/publicBackground/indexRes/background",userPath+"/background");
        copyFile.byteCopy(basePath+"/publicBackground/indexRes/head/head.png",userPath+"/head_img/head.png");
    }
}
